package com.lungle.warmdiary.app.view.me.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.lungle.warmdiary.R;
import com.lungle.warmdiary.app.base.BaseFragment;
import com.lungle.warmdiary.app.ui.InputItemView;
import com.umeng.analytics.MobclickAgent;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by God on 2016/8/8.
 */
public class ChangeWechatNicknameFragment extends BaseFragment {
    private static final String CHANGE_WECHAT_USERNAME_SCREEN = "change wechat username screen";
    private final int CODE_SELECT = 1;
    public static final String VALUE_USERNAME = "nickname";
    @Bind(R.id.itv_friend)
    InputItemView mItvFriend;
    private String mUsername;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_wechat_username, container, false);
        initData();
        initViews(view);
        return view;
    }


    @Override
    public void initData() {
        mUsername = getArguments().getString(VALUE_USERNAME);
    }

    private void initViews(View view) {
        ButterKnife.bind(this, view);
        if (!TextUtils.isEmpty(mUsername)) {
            mItvFriend.setText(mUsername);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(CHANGE_WECHAT_USERNAME_SCREEN);
        MobclickAgent.onResume(getActivity());

    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(CHANGE_WECHAT_USERNAME_SCREEN);
        MobclickAgent.onPause(getActivity());

    }


    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        getActivity().getMenuInflater().inflate(R.menu.active_detail, menu);
        menu.findItem(R.id.login_menu_send);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        setResultData(mItvFriend.getInputText());
        getActivity().finish();

        return true;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }


    private void setResultData(String username) {
        Intent intent = new Intent();
        intent.putExtra(VALUE_USERNAME, username);
        getActivity().setResult(MediaAddResultFragment.RESULT_OK, intent);
    }


}
